<?php
require('./juslr/log.php');
require('../cofd/functions.php');
session_start(); 
if (isset($_SESSION['authorized_s']) && $_SESSION['authorized_s'] === true && time() < $_SESSION['expires_at_s']) {
    session_write_close();
    echo "<script>alert('当前你已登录'); window.location.href = './';</script>"; 
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = isset($_POST['accountl']) ? $_POST['accountl'] : null;
    $password = isset($_POST['passwordl']) ? $_POST['passwordl'] : null;
    if($name == null || $password == null){
        $zcname = isset($_POST['account']) ? $_POST['account'] : null;
        $zcpass = isset($_POST['password']) ? $_POST['password'] : null;
        $mail = isset($_POST['mail']) ? $_POST['mail'] : null;
        require('./juslr/reg.php');
        reguser($conn,$zcname, $zcpass, $mail,x_real_ip());
    }else{
        getlogin($name, $password);
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登/注</title>
    <link rel="stylesheet" href="/ast/layui/css/layui.css">
    <link rel="stylesheet" href="/ast/login-user/loreg.css">
    </head>
<body>
<div class="login-container">
    <header id="headery" class="site-title">登录</header>
    <div class="form-container">
<form class="login-form" action="./?juslr" method="post">
            <input type="text" id="louser" name="accountl" placeholder="账号-Username" required>
            <input type="password" id="lopass" name="passwordl" placeholder="密钥-Key" required>
            <input type="text" name = "bzf_logor" style="display: none;" value="log">
            <button class="login-btn" type="submit" >登录</button>
</form>
        <form class="register-form" action="./?juslr" method="post" style="display: none;">
            <input type="text" name="account" id="zcuser" placeholder="账号-Username" required>
            <input type="password" name="password" id="zcpass" placeholder="密钥-Key" required>
            <div class="captcha-container">
        <input type="text" id="accoutcode"  name="yzm" placeholder="验证码" required class="captcha-input">
        <div onclick="createCode()" class="captcha-display"></div>
    </div>
    <input type="text" id="mail"  name="mail" placeholder="输入邮箱地址" required class="captcha-input">
    <input type="text" id="mail_code" style="display: none;" name="mail2" placeholder="输入邮箱验证码" required class="captcha-input">
    <input type="text" name = "bzf_logor" style="display: none;" value="zc">
    <button type="button" class="captcha-refresh-btn" id="getmail"  onclick="createmailCode()">获取邮箱验证码</button>
    <button class="register-btn" type="submit" id="regbuttontos">注册</button>
</form>
    </div>
    <div class="form-toggle">
        <button class="toggle-btn" data-target="login">登录</button>
        <button class="toggle-btn" data-target="register">注册</button>
    </div>
</div>
<script src="/ast/layui/layui.js"></script> 
<script>
const mailcode = Math.random().toString(36).substr(2, 6).toUpperCase(); 
document.addEventListener('DOMContentLoaded', function() {
    const loginForm = document.querySelector('.login-form');
    const registerForm = document.querySelector('.register-form');
    const toggleButtons = document.querySelectorAll('.form-toggle .toggle-btn');

    function switchForm(target) {
        if (target === 'login') {
            loginForm.style.display = 'block';
            registerForm.style.display = 'none';
            document.getElementById("headery").innerHTML="登录";
        } else {
            loginForm.style.display = 'none';
            document.getElementById("headery").innerHTML="注册";
            registerForm.style.display = 'block';
        }
    }

    toggleButtons.forEach(button => {
        button.addEventListener('click', function() {
            const target = this.getAttribute('data-target');
            switchForm(target);
            toggleButtons.forEach(btn => btn.classList.remove('active'));
            this.classList.add('active');
        });
    });
});
function createCode() {
    const code = Math.random().toString(36).substr(2, 6).toUpperCase(); 
    document.querySelector('.captcha-display').textContent = code;
}
function createmailCode() {
    var mail = document.getElementById("mail").value;
    var mailbu = document.getElementById("getmail");
    var mailcode_i = document.getElementById("mail_code");
    if(mail === "" ){
        layer.msg('邮箱不能为空');
        return false;
}
      fetch('juslr/mailsend.php?to='+mail+'&subject=注册验证码&message=注册验证码：'+mailcode)
    .then(function (response) {
        return response.json();
    })
    .then(function (data) {
        if (data.code === 400) {
            layer.msg('邮箱验证码发送失败，请重新输入邮箱');
}else{
      layer.msg('验证码已发送至邮箱，请注意查收');
}
    })
      mailbu.style.display = "none";
      mailcode_i.style.display = "block";
        }
document.addEventListener('DOMContentLoaded', function() {
    createCode(); 
});
document.getElementById('regbuttontos').addEventListener('submit', function(event) {
    event.preventDefault();
    var post_user = document.getElementById("zcuser").value;  
    var post_pass = document.getElementById("zcpass").value;  
    var accoutcode = document.getElementById("accoutcode").value; 
    var mailcode2 = document.getElementById("mail_code").value; 
    var mailz = document.getElementById("mail").value; 
    if(accoutcode != document.querySelector('.captcha-display').textContent){
        layer.msg("验证码错误");
        createCode();
        return false;
    }
    if(post_user === "" || post_pass === ""){
        layer.msg("账号或密码不能为空");
        createCode();
        return false;
    }
    if(mailcode2 === "" || mailcode2 != mailcode){
        layer.msg("邮箱验证码不能为空或错误");
        createCode();
        return false;
    }
    document.getElementById('regbuttontos').submit();
});

</script>
</body>
</html>